<template>
  <!--列表-->
  <div class="list-container">
    <div class="sortList clearfix">
      <div class="center">
        <!--banner轮播-->
        <div class="swiper-container" id="mySwiper">
          <div class="swiper-wrapper">
            <div class="swiper-slide" v-for="item in banner" :key="item.id">
              <!-- 此时出现找不到图片,原因是路径不对,在页面加载后,在index.html,的入口开始查找图片,而图片的地址是./images,所以需要在index.html同级创建一个images文件夹 -->
              <img :src="item.imgUrl" />
            </div>
          </div>
          <!-- 如果需要分页器 -->
          <div class="swiper-pagination"></div>

          <!-- 如果需要导航按钮 -->
          <div class="swiper-button-prev"></div>
          <div class="swiper-button-next"></div>
        </div>
      </div>
      <div class="right">
        <div class="news">
          <h4>
            <em class="fl">尚品汇快报</em>
            <span class="fr tip">更多 ></span>
          </h4>
          <div class="clearix"></div>
          <ul class="news-list unstyled">
            <li><span class="bold">[特惠]</span>备战开学季 全民半价购数码</li>
            <li><span class="bold">[公告]</span>备战开学季 全民半价购数码</li>
            <li><span class="bold">[特惠]</span>备战开学季 全民半价购数码</li>
            <li><span class="bold">[公告]</span>备战开学季 全民半价购数码</li>
            <li><span class="bold">[特惠]</span>备战开学季 全民半价购数码</li>
          </ul>
        </div>
        <ul class="lifeservices">
          <li class="life-item">
            <i class="list-item"></i>
            <span class="service-intro">话费</span>
          </li>
          <li class="life-item">
            <i class="list-item"></i>
            <span class="service-intro">机票</span>
          </li>
          <li class="life-item">
            <i class="list-item"></i>
            <span class="service-intro">电影票</span>
          </li>
          <li class="life-item">
            <i class="list-item"></i>
            <span class="service-intro">游戏</span>
          </li>
          <li class="life-item">
            <i class="list-item"></i>
            <span class="service-intro">彩票</span>
          </li>
          <li class="life-item">
            <i class="list-item"></i>
            <span class="service-intro">加油站</span>
          </li>
          <li class="life-item">
            <i class="list-item"></i>
            <span class="service-intro">酒店</span>
          </li>
          <li class="life-item">
            <i class="list-item"></i>
            <span class="service-intro">火车票</span>
          </li>
          <li class="life-item">
            <i class="list-item"></i>
            <span class="service-intro">众筹</span>
          </li>
          <li class="life-item">
            <i class="list-item"></i>
            <span class="service-intro">理财</span>
          </li>
          <li class="life-item">
            <i class="list-item"></i>
            <span class="service-intro">礼品卡</span>
          </li>
          <li class="life-item">
            <i class="list-item"></i>
            <span class="service-intro">白条</span>
          </li>
        </ul>
        <div class="ads">
          <img src="./images/ad1.png" />
        </div>
      </div>
    </div>
  </div>
</template>
<script src="dist/js/swiper-bundle.min.js"></script>
<script>
import { mapState } from "vuex"
import Swiper from "swiper"

export default {
  name: "ListContainer",
  computed: {
    //计算属性,从store中映射过来的数据,在此时使用相当于在本组件中的data中一样,可以直接使用
    ...mapState("home", ["banner"]),
  },
  watch: {
    banner() {
      // 为什么使用swiper会出现问题,原因是一上来,页面就要加载渲染图片,加载完成时机不确定(图片是服务器端返回的,是一个异步的任务,无法确保渲染的时机在页面挂载完毕之前),但是代码会继续执行下去,此时,swiper实例有可能已经创建完毕,

      // 所以解决办法,思路必须是让swiper实例创建在图片的dom节点渲染完成后创建.这样swiper实例才能生效

      // 完美解决方法,不需要使用生命周期钩子函数,在组件中添加watch侦听器,一旦页面中被侦听的图片的dom渲染完毕,立刻执行$nextTick内的回调函数
      // 恰好可以将swiper实例放在这个回调函数中,这样可以等待页面中的图片渲染完毕后,立刻执行创建swiper实例,将图片进行轮播
      this.$nextTick(() => {
        new Swiper(".swiper-container", {
          slidesPerView: 1,
          spaceBetween: 30,
          loop: true,
          autoplay: {
            delay: 1000,
            stopOnLastSlide: false,
            disableOnInteraction: true,
          },
          pagination: {
            el: ".swiper-pagination",
            clickable: true,
          },
          navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
          },
        })
      })
    },
  },
  mounted() {
    this.$store.dispatch("home/getbannerListData")
    // 如果直接在挂载完成后,使用swiper插件实例,此时会出现一个问题,由于渲染页面的时机是不确定的(图片是服务器端返回的,是一个异步的任务,无法确保渲染的时机在页面挂载完毕之前),但是在页面挂载完毕后,就会执行swiper插件的轮播效果,因此我们需要将swiper的执行时机退后,使用一个计时器来将他变成一个异步任务,但是缺点很明显的是,无法确定页面的图片是否真的渲染完毕.
    /** 解决方法一:使用定时器,将swiper变成异步,但是无法确定页面的图片是否真的渲染完毕.
       *
      // setTimeout(() => {
      //   new Swiper(".swiper-container", {
      //     // direction: "vertical", // 垂直切换选项
      //     loop: true, // 循环模式选项
      //     // 如果需要分页器
      //     pagination: {
      //       el: ".swiper-pagination",
      //     },
      //     // 如果需要前进后退按钮
      //     navigation: {
      //       nextEl: ".swiper-button-next",
      //       prevEl: ".swiper-button-prev",
      //     },
      //     // 如果需要滚动条
      //     scrollbar: {
      //       el: ".swiper-scrollbar",
      //     },
      //   })
      // }, 1000)
       */
  },
  updated() {
    /**解决方法二: 将swiper插件放在updated钩子函数中,缺点是每次页面更新都会重新创建一个swiper实例对象,会产生大量的内存消耗
     */
    // new Swiper(".swiper-container", {
    //   slidesPerView: 1,
    //   spaceBetween: 30,
    //   loop: true,
    //   autoplay: {
    //     delay: 1000,
    //     stopOnLastSlide: false,
    //     disableOnInteraction: true,
    //   },
    //   pagination: {
    //     el: ".swiper-pagination",
    //     clickable: true,
    //   },
    //   navigation: {
    //     nextEl: ".swiper-button-next",
    //     prevEl: ".swiper-button-prev",
    //   },
    // })
  },
}
</script>

<style lang="less" scoped>
.list-container {
  width: 1200px;
  margin: 0 auto;

  .sortList {
    height: 464px;
    padding-left: 210px;

    .center {
      box-sizing: border-box;
      width: 740px;
      height: 100%;
      padding: 5px;
      float: left;
    }

    .right {
      float: left;
      width: 250px;

      .news {
        border: 1px solid #e4e4e4;
        margin-top: 5px;

        h4 {
          border-bottom: 1px solid #e4e4e4;
          padding: 5px 10px;
          margin: 5px 5px 0;
          line-height: 22px;
          overflow: hidden;
          font-size: 14px;

          .fl {
            float: left;
          }

          .fr {
            float: right;
            font-size: 12px;
            font-weight: 400;
          }
        }

        .news-list {
          padding: 5px 15px;
          line-height: 26px;

          .bold {
            font-weight: 700;
          }
        }
      }

      .lifeservices {
        border-right: 1px solid #e4e4e4;
        overflow: hidden;
        display: flex;
        flex-wrap: wrap;

        .life-item {
          border-left: 1px solid #e4e4e4;
          border-bottom: 1px solid #e4e4e4;
          margin-right: -1px;
          height: 64px;
          text-align: center;
          position: relative;
          cursor: pointer;
          width: 25%;

          .list-item {
            background-image: url(./images/icons.png);
            width: 61px;
            height: 40px;
            display: block;
          }

          .service-intro {
            line-height: 22px;
            width: 60px;
            display: block;
          }

          &:nth-child(1) {
            .list-item {
              background-position: 0px -5px;
            }
          }

          &:nth-child(2) {
            .list-item {
              background-position: -62px -5px;
            }
          }

          &:nth-child(3) {
            .list-item {
              background-position: -126px -5px;
            }
          }

          &:nth-child(4) {
            .list-item {
              background-position: -190px -5px;
            }
          }

          &:nth-child(5) {
            .list-item {
              background-position: 0px -76px;
            }
          }

          &:nth-child(6) {
            .list-item {
              background-position: -62px -76px;
            }
          }

          &:nth-child(7) {
            .list-item {
              background-position: -126px -76px;
            }
          }

          &:nth-child(8) {
            .list-item {
              background-position: -190px -76px;
            }
          }

          &:nth-child(9) {
            .list-item {
              background-position: 0px -146px;
            }
          }

          &:nth-child(10) {
            .list-item {
              background-position: -62px -146px;
            }
          }

          &:nth-child(11) {
            .list-item {
              background-position: -126px -146px;
            }
          }

          &:nth-child(12) {
            .list-item {
              background-position: -190px -146px;
            }
          }
        }
      }

      .ads {
        margin-top: 5px;

        img {
          opacity: 0.8;
          transition: all 400ms;

          &:hover {
            opacity: 1;
          }
        }
      }
    }
  }
}
</style>
